<template>
  <div class="login-container">
    <el-form :model="loginForm" ref="loginForm" label-width="120px">
      <el-form-item label="用户名" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]">
        <el-input v-model="loginForm.username" placeholder="请输入用户名" />
      </el-form-item>

      <el-form-item label="密码" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
        <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    };
  },
  methods: {
    async handleLogin() {
      try {
        // 模拟 API 请求，假设返回一个 token
        const response = await this.$axios.post('/auth/login', this.loginForm); // 后端验证接口
        localStorage.setItem('token', response.data.token); // 保存 token 到 localStorage
        this.$router.push('/order'); // 登录成功后跳转到订单管理页面
      } catch (error) {
        this.$message.error('登录失败，请检查用户名和密码');
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  width: 300px;
  margin: 100px auto;
}
</style>
